package com.common.www.bean.entity.recycle;

//import com.bianmaba.realestate.bean.entity.authorization.UserRoleMiddle;

import com.common.www.MyApplicationListener;
import org.springframework.boot.base.jpa.domain.annotations.QueryCacheable;
import org.springframework.boot.base.jpa.domain.entity.NumberIdentityEntity;
import com.common.www.bean.entity.auth.AuthUser;
import com.common.www.bean.entity.sale.SaleBill;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import java.math.BigDecimal;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import io.swagger.annotations.ApiModel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * 回收单对象
 *
 * @author luozisong
 */
@DynamicUpdate(true)
@DynamicInsert(true)
@Entity
@Getter
@Setter
@QueryCacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Table()
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "project返回参数")
public class RecycleBill extends NumberIdentityEntity {

    @JsonIgnoreProperties(value = {"authRoles", "company"})
    @ManyToOne()
    private AuthUser createUser;

    @JsonIgnoreProperties(value = {"authRoles", "company"})
    @ManyToOne()
    private AuthUser receiveUser;

    /**
     * 当前会所属的品类
     */
    @JsonIgnoreProperties(value = {"bill"})
    //二级分类
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "bill")
    private List<RecycleBillTypeItem> typeList;
    /**
     * 携带的图片信息
     */
    @JsonIgnoreProperties(value = {"bill"})
    //二级分类
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "bill")
    private List<RecycleBillImgItem> imgItemList;

    /**
     * 被关联到的卖出单
     */
    @JsonIgnoreProperties(value = {"typeList"})
    @ManyToOne()
    @JoinColumn(name = "parent")
    private SaleBill saleBill;

    /**
     * 状态 0.已创建待接单 1.已接单待上门 10.已完成未卖出（已支付完成）  11.收货已完成并关联到收货单
     */
    @Column(length = 11)
    private Integer status;
    /**
     * 支付方式 1.现金支付 2.转账支付
     */
    @Column(length = 11)
    private Integer payType;
    /**
     * 回收的位置标题,小区名称或某个建筑的名称,主要用户快速区分
     */
    @Column(length = 64)
    private String addressTitle;
    /**
     * 回收的位置标题
     */
    @Column(length = 512)
    private String address;
    /**
     * 地址的纬度,采用GCJ02坐标系为准
     */
    @Column(length = 15)
    private Double lat;
    /**
     * 地址的经度,采用GCJ02坐标系为准
     */
    @Column(length = 15)
    private Double lng;

    /**
     * 单位类型 1.元/吨 2.元/KG 3.个
     */
    @Column(length = 11)
    private Integer unitType;

//    /**
//     * 价格的单位
//     */
//    @Column(length = 11)
//    private String priceUnit;
    /**
     * 预估重量
     */
    @Column(length = 11)
    private String estimateWeight;
    /**
     * 本次卖货人手机号
     */
    @Column(length = 11)
    private String phone;
    /**
     * 本次收货点位置类型 1.地面 2.地下室
     */
    @Column(length = 11)
    private Integer locationType;
    /**
     * 限高高度
     */
    @Column(length = 11)
    private Integer heightLimit;

    /**
     * 实际卖出总金额
     */
    @Column(precision = 23, scale = 2)
    private BigDecimal priceActual;
    /**
     * 实际称的总重
     */
    @Column(precision = 23, scale = 2)
    private BigDecimal weightActual;

    /**
     * 卖货人签字图片OSS的对象名称
     */
    @Column(length = 128)
    private String signImg;

    /**
     * 备注
     */
    @Column(length = 512)
    private String remark;

    @Transient
    private String unitTypeName;
    @Transient
    private String statusName;

    public String getStatusName() {
        if (status == null) {
            return "未知";
        }
        if (status == 0) {
            return "待接单";
        } else if (status == 1) {
            return "已接单";
        } else if (status == 10) {
            return "待卖出";
        } else if (status == 11) {
            return "已完成";
        } else {
            return "未知";
        }
    }

    public String getSignImgUrl() {
        return MyApplicationListener.projectPath + signImg;
    }

    public String getUnitTypeName() {
        if (unitType == null) {
            return "未知";
        }
        if (unitType == 1) {
            unitTypeName = "元/吨";
        } else if (unitType == 2) {
            unitTypeName = "元/KG";
        } else if (unitType == 3) {
            unitTypeName = "元/个";
        }
        return unitTypeName;
    }

}